package edu.sandau.basic.service.impl;

import edu.sandau.basic.domain.User;
import edu.sandau.basic.global.GlobalEnum;
import edu.sandau.basic.mapper.UserExtMapper;
import edu.sandau.basic.service.UserService;
import edu.sandau.basic.utils.Converter;
import edu.sandau.basic.vo.UserVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    //private UserMapper userMapper;
    private UserExtMapper userExtMapper;
    @Override
    public GlobalEnum.ErrorCode addUser(UserVo userVo){
        GlobalEnum.ErrorCode errorCode = GlobalEnum.ErrorCode.USER_ADD_FAILED;
        
        // 检查用户名是否已存在
        User existingUser = userExtMapper.selectByUserName(userVo.getUserName());
        if (existingUser != null) {
            return GlobalEnum.ErrorCode.USER_ALREADY_EXISTS;
        }
        
        User user = new User();
        user.setUsername(userVo.getUserName());
        user.setPassword(userVo.getPassword());
        user.setBirthday(Converter.localDateToDate(userVo.getBirthday()));
        user.setMobile(userVo.getMobile());
        user.setEnName(userVo.getEnName());
        user.setEmail(userVo.getEmail());
        user.setIsActive(true);
        user.setProfileUrl(userVo.getProfileUrl());
        user.setCreateperson(1);
        user.setUpdateperson(1);
        user.setCreatetime(Converter.localDateTimeToDate(LocalDateTime.now()));
        user.setUpdatetime(Converter.localDateTimeToDate(LocalDateTime.now()));
    // 使用 insertSelective 只插入非空字段，避免向 id 等列插入 null 导致数据库错误
    int result = userExtMapper.insertSelective(user);
        if(result == 1){
            errorCode = GlobalEnum.ErrorCode.OK;
        }
        return errorCode;
    }

    @Override
    public GlobalEnum.ErrorCode userLogin(String username, String password) {
        //userMapper.
        //去数据库里面根据用户名找出密码，判断密码是否一致？
        User user = userExtMapper.selectByUserName(username);
        //User user = null;
        if(user != null && user.getPassword().equals(password)){
            return GlobalEnum.ErrorCode.OK;
        }
        return GlobalEnum.ErrorCode.USER_NOT_EXIST;
    }

    @Override
    public List<User> getAllUsers() {
        return userExtMapper.selectAllUsers();
    }

    @Override
    public GlobalEnum.ErrorCode deleteUserByUsername(String username) {
        // 先检查用户是否存在
        User user = userExtMapper.selectByUserName(username);
        if (user == null) {
            return GlobalEnum.ErrorCode.USER_NOT_EXIST;
        }
        
        // 删除用户
        int result = userExtMapper.deleteByUsername(username);
        if (result > 0) {
            return GlobalEnum.ErrorCode.OK;
        } else {
            return GlobalEnum.ErrorCode.UNKNOWN;
        }
    }

    @Override
    public GlobalEnum.ErrorCode updateUserByUsername(String username, User user) {
        // 先检查用户是否存在
        User existingUser = userExtMapper.selectByUserName(username);
        if (existingUser == null) {
            return GlobalEnum.ErrorCode.USER_NOT_EXIST;
        }
        
        // 设置更新时间和更新人
        user.setUpdatetime(Converter.localDateTimeToDate(LocalDateTime.now()));
        user.setUpdateperson(existingUser.getUpdateperson());
        
        // 更新用户信息
        int result = userExtMapper.updateByUsername(username, user);
        if (result > 0) {
            return GlobalEnum.ErrorCode.OK;
        } else {
            return GlobalEnum.ErrorCode.UNKNOWN;
        }
    }

    @Override
    public User getUserByUsername(String username) {
        if (username == null || username.trim().isEmpty()) {
            return null;
        }
        return userExtMapper.selectByUserName(username);
    }
}
